package com.bawei.library_widget;

import android.graphics.Rect;
import android.util.Log;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/**
 * @Author : yaotianxue
 * @Time : On 2023/5/30 15:23
 * @Description : CustomDividerDecration
 */
public class CustomItemDecoration extends RecyclerView.ItemDecoration {
    private int count;//列数

    public CustomItemDecoration(int count, int space) {
        this.count = count;
        this.space = space;
    }

    private int space;//间隔

    @Override
    public void getItemOffsets(@NonNull Rect outRect, @NonNull View view, @NonNull RecyclerView parent, @NonNull RecyclerView.State state) {
     //   super.getItemOffsets(outRect, view, parent, state);
        int position = parent.getChildAdapterPosition(view);//当前item的下标
        outRect.top = space;
        if(position%count == 0){//最左边
            outRect.left = space;
            outRect.right = space/2;
        }else if(position%count == count-1){//最右边
            outRect.left = space/2;
            outRect.right = space;
        }else{
            outRect.left = space/2;
            outRect.right = space/2;
        }
        //判断是否为最后一行
        int total=  parent.getAdapter().getItemCount()/count;
        Log.d("ytx", "总行数: "+total);
        if(position/count == total-1){
            outRect.bottom = space;
        }



    }
}
